﻿// -------------------------------------------------------------------------
//    @FileName         :    NFFashionUtil.cpp
//    @Author           :    gaoyi
//    @Date             :    2025-03-28
//    @Email			:    445267987@qq.com
//    @Module           :    NFFashionUtil
//
// -------------------------------------------------------------------------

#include "NFFashionUtil.h"

#include "NFLogicCommon/BagPage/NFBagPage.h"
#include <NFFashionPart.h>
#include <NFHeroPart.h>
#include <NFPackagePart.h>
#include <NFPlayer.h>
#include <NFPlayerMgr.h>
#include <NFSkillPart.h>
#include <TransferDescEx.h>
#include <DescStore/ActorDesc.h>
#include <DescStore/EquipDesc.h>
#include <DescStore/TransferDesc.h>

int NFFashionUtil::GetEquipIdxByFashionIdx(int iIdx)
{
    switch (iIdx)
    {
        case 0:
            return 0;
        case 1:
            return 1;
        case 2:
            return 3;
        case 3:
            return 4;
        default:
            break;
    }

    return -1;
}

int NFFashionUtil::GetDefaultFashionID(const tbRoleDetail& stRoleDetail, int iIdx)
{
    return 0;
}

int NFFashionUtil::GetDefaultFashionID(uint64_t ullRoleId, int iIdx)
{
    NFPlayer* pPlayer = NFPlayerMgr::Instance()->GetPlayer(ullRoleId);
    CHECK_EXPR_RETURN(pPlayer);
    NFFashionPart* pExt = pPlayer->GetPart<NFFashionPart>();
    CHECK_EXPR_RETURN(pExt);
    NFSkillPart* pSkillExt = pPlayer->GetPart<NFSkillPart>();
    CHECK_EXPR_RETURN(pExt);

    if (pExt->GetTransfer() &&
        pSkillExt->GetData()->transfer_id > 0)
    {
        const TTransferFashion* pstOldFashion = TransferDescEx::Instance()->GetTransferFashion(pSkillExt->GetData()->transfer_id);
        if (pstOldFashion)
        {
            switch (iIdx)
            {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                    return pstOldFashion->m_aiFashionID[iIdx];
                default:
                    break;
            }
        }
    }
    else
    {
        NFHeroPart* pHeroExt = pPlayer->GetPart<NFHeroPart>();
        CHECK_EXPR_RETURN(pHeroExt);

        auto pHeroDesc = ActorDesc::Instance()->GetDesc(pHeroExt->GetHeroID());
        CHECK_EXPR_RETURN(pHeroDesc);

        switch (iIdx)
        {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                return pHeroDesc->default_fashion_id[iIdx];
            default:
                break;
        }
    }

    return 0;
}

int NFFashionUtil::GetEquipFashionID(const tbRoleDetail& stRoleDetail, int iIdx)
{
    return 0;
}

int NFFashionUtil::GetEquipFashionID(uint64_t ullRoleId, int iIdx)
{
    NFPlayer* pPlayer = NFPlayerMgr::Instance()->GetPlayer(ullRoleId);
    CHECK_EXPR_RETURN(pPlayer);

    NFHeroPart* pHeroExt = pPlayer->GetPart<NFHeroPart>();
    CHECK_EXPR_RETURN(pHeroExt);

    HeroOne* pHero = pHeroExt->GetHero();
    CHECK_EXPR_RETURN(pHero);

    NFPackagePart* pItemExt = pPlayer->GetPart<NFPackagePart>();
    CHECK_EXPR_RETURN(pItemExt);

    int iFashionId = 0;
    int iEquipIdx = GetEquipIdxByFashionIdx(iIdx);
    if (iEquipIdx >= 0 && iEquipIdx < (int)ARRAYSIZE(pHero->equip_list))
    {
        uint64_t ullEquipID = pHero->equip_list[iEquipIdx];
        if (ullEquipID != 0)
        {
            auto pEquip = pItemExt->GetItemByUUID(EN_BPT_DRESSED_ITEM, ullEquipID);
            CHECK_EXPR_RETURN(pEquip);

            auto pDesc = EquipDesc::Instance()->GetDesc(pEquip->GetItemID());
            CHECK_EXPR_RETURN(pDesc);

            iFashionId = pDesc->default_fashion_id;

            LOGSVR_DEBUG("player: " << ullRoleId <<
                " hero id: " << pHeroExt->GetHeroID() <<
                " equip: " << ullEquipID <<
                " desc id: " << pEquip->GetItemID() <<
                " part: " << pDesc->part <<
                " index: " << iIdx <<
                " fashion id: " << pDesc->default_fashion_id);
        }
    }

    if (iFashionId == 0)
    {
        iFashionId = GetDefaultFashionID(ullRoleId, iIdx);
    }

    LOGSVR_DEBUG("player: " << ullRoleId <<
        " index: " << iIdx <<
        " fashion id: " << iFashionId)

    return iFashionId;
}
